import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;

import dbcTools.DBC_MySQL;
import dbcTools.DB_Operation;

/**
 * 将本地文件的图片传到数据库的test的image表中并下载到本机桌面
 */
class Test2{
	ArrayList<Object> lhs;
	public Test2(){
		lhs = new ArrayList<>();
	}
	public void setLHS(ArrayList<Object> lhs) {
		this.lhs = lhs;
	}
}
//class Test3{
//	LinkedHashSet<Object> lhs;
//	public Test2(){
//		lhs = new LinkedHashSet<>();
//	}
//	public void setLHS(LinkedHashSet<Object> lhs) {
//		this.lhs = lhs;
//	}
//}

public class Test1 {

	private static String url = "jdbc:mysql://localhost:3306/my_wechat";
	private static String user = "root";
	private static String password = "123456";
	private static Connection con;

	public String test1() {
		System.out.println("test1");
		return "test1";
	}

	public Test1 test2(String str) {
		System.out.println(str + " test2");
		return this;
	}

	public static void main(String[] args) throws Exception {
//	    Class.forName("com.mysql.jdbc.Driver");
//	    con=DriverManager.getConnection(url,user,password);
//	    shangchuan();
//	    xiazai();    
//	    Test1 t = new Test1();
//		t.test2("a").test2("b").test2(new Test1().test1());

//		Test1 t1 = new Test1();
//		Test1 t2 = new Test1();
//		ArrayList<Test1> al = new ArrayList<>();
//		al.add(t1);
//		al.add(t2);
//		al.add(t1);
//		System.out.println(al.size());
//		System.out.println(al.get(0));
//		System.out.println(al.get(1));
//		System.out.println(al.get(2));
		
//		Test1 t1 = new Test1();
//		Test1 t2 = new Test1();
//		LinkedHashSet<Test1> lhs = new LinkedHashSet<>();
//		lhs.add(t1);
//		lhs.add(t2);
//		lhs.add(t1);
//		lhs.add(t2);
//		System.out.println(lhs.size());
//		Iterator<Test1> iter = lhs.iterator();
//		System.out.println(iter.next());
//		System.out.println(iter.next());
//		lhs.clear();
//		System.out.println(lhs.size());
		
//		Test2 t = new Test2();
//		t.lhs.add(new Object());
//		Test2 t1 = new Test2();
//		t1.setLHS(t.lhs);
//		t1.lhs.add(new Object());
//		System.out.println(t.lhs.size());
		
//		LinkedHashSet<Object> lhs = new LinkedHashSet<>();
//		LinkedHashSet<Object> lhs1 = lhs;
//		lhs1.add(new Object());
//		System.out.println(lhs.size());
//		System.out.println(lhs1.size());
	  
		DBC_MySQL dbc = new DBC_MySQL("config.xml");
		Connection connection = dbc.getConnection();
		System.out.println(connection);
//		
		DB_Operation dbo = new DB_Operation(connection, "user");
		ResultSet resultSet = dbo.selectOnly(
					dbo.prepareSubOperation().exists(
							dbo.prepareSubOperation().select("id").where("id", "=", 2).getSql()
					).getSql()
				).executeQuery();
		while(resultSet.next()) {
			System.out.println(resultSet.getBoolean(1));
		}
		
//		ResultSet resultSet = dbo
//				.select("*").executeSelect();
//		while (resultSet.next()) {
//			System.out.println(resultSet.getBoolean(1));
//		}
//		System.out.println(dbo.select("*").getSql());
  }

	// 添加图片到数据库test4的file表
	public static void shangchuan() throws Exception {
		String sql = "insert into image(name,content) values(?,?)";
		PreparedStatement ptmt = con.prepareStatement(sql);
		ptmt.setString(1, "美女.jpg");
		InputStream is = null;
		is = new FileInputStream("1.jpg");
		ptmt.setBinaryStream(2, is, is.available());
		// 方法说明：PreparedStatement.setBinaryStream(int parameterIndex, InputStream x, int
		// length)
		ptmt.execute();
		System.out.println("图片添加成功！");

	}

	// 从数据库中把图片下载至桌面
	public static void xiazai() throws Exception {
		String sql = "select content from image where id=3";// 在我这里3.jpg是第三张图片
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery(sql);// 将查询结果给rs
		if (rs.next()) {
			InputStream is = rs.getBinaryStream("content");
			// .getBinaryStream():a Java input stream that delivers the database column
			// value as a stream of uninterpreted bytes
			FileOutputStream fos = new FileOutputStream("美女.jpg");
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len);// 将数据库的图片写出
			}
			System.out.println("下载成功！已下载至桌面，请查看");
		} else {
			System.out.println("图片不存在！");
		}
		con.close();
	}
}